<?php

class View extends CI_Controller{

	function index()
	{
            
        }
        function publicView($param_rec_sit)
        {
            //$param_rec_sit='79';//$_POST['sit_id'];
            //$param_rec_sit='352';
            $this->load->model('Situation');
            $query=$this->Situation->getMySituation_html($param_rec_sit);
            $sit_data=array();
            foreach ($query->result_array() as $row)
            {
                    $sit_name=$row['name'];
                    $sit_id=$row['id'];
                    $height=$row['html'];
                    $sit_context_count=$row['context_count'];
                    $sit_decs=$row['description'];//desc
                    $sit_privacy=$row['privacyType'];//privacy
                    $back_img=$row['background_image'];
                    $sit_data = array(
                        'name' => $sit_name,
                        'id' => $sit_id,
                        'UserProfile_email' => 'mmm@d.c',
                        'description' =>$sit_decs,
                        'privacyType'=>$sit_privacy,
                        'context_count' => $sit_context_count
                    );
            }
            //$html='salem alkzhdsini';

            $this->load->model('Context');
            $query=$this->Context->getMyContext($param_rec_sit);
            $string_of_html='';
            foreach ($query->result_array() as $row)
            {
                    $name=$row['name'];
                    $tags=$row['tags'];
                    $html=$row['html'];
                    $color=$row['color'];
                    $id=$row['id'];
                    $sit_id=$row['situation_Id'];
                    $flag=$row['flag'];
                    $cflag=explode ( "_" , $flag );
                    if($cflag[0]=="duplicate"){
                                $to_load_id=$cflag[1];
                                //echo $to_load_id;
                                $dcontext=$this->Context->getContext($to_load_id);
                                if($dcontext->num_rows() !=0){
                                    $dcontext=$dcontext->row();
                                    $dhtmlt=$dcontext->html;
                                    $this->load->library('simple_html_dom');
                                    $dhtml=new simple_html_dom();
                                    $dhtml->load($dhtmlt);
                                    //The current duplicated context
                                    $complete_html=new simple_html_dom();
                                    $complete_html->load($html);
                                    //name of outer of current
                                    $cname="o_".$name;
                                    //name of inner of original
                                    $dvname=$dcontext->name;
                                    $ec=$complete_html->find("div[id=$cname]",0);
                                    $ed=$dhtml->find("div[id=$dvname]",0);
                                    //echo $dvname;
                                    $newHTML= str_replace($dvname, $name, $ed->outertext);
                                    $ec->innertext=$newHTML . $ec->innertext;
                                    $html=$complete_html->save();
                                }
                                else{
                                    $this->Context->deleteContext($id);
                                    $cflag[0]!="del";
                                }
                            }
                            if($cflag[0]!="del")
                                $string_of_html=$string_of_html.$html;
            }
            //$sit_json=json_encode($sit_data);
            //$context_json=json_encode($array_of_context);

            $data=array(
                'html' => $string_of_html,
                'back_img'=>$back_img,
                'height'=>$height
            );


            /*$passed_situation='<div>hello</div>';
            $data=array(
                'html' => $passed_situation
            );*/
            $this->load->view('viewpage',$data);
        }

        function friendView($param_rec_sit)
	{
                //$param_rec_sit='374';//$_POST['situation_id_to_retrieve_html'];
		$this->load->model('Situation');
		$query=$this->Situation->getMySituation_html($param_rec_sit);
                $sit_data=array();
		foreach ($query->result_array() as $row)
		{
			$sit_name=$row['name'];
			$sit_id=$row['id'];
			$height=$row['html'];
                        $sit_context_count=$row['context_count'];
                        $sit_decs=$row['description'];//desc
                        $sit_privacy=$row['privacyType'];//privacy
                        $back_img=$row['background_image'];
                        $sit_data = array(
                            'name' => $sit_name,
                            'id' => $sit_id,
                            'UserProfile_email' => 'mmm@d.c',
                            'description' =>$sit_decs,
                            'privacyType'=>$sit_privacy,
                            'context_count' => $sit_context_count

                        );
		}
		//$html='salem alkzhdsini';

                $this->load->model('Context');
		$query=$this->Context->getMyContext($param_rec_sit);
                $array_of_context=array();
                $all_html_contxs='';
                foreach ($query->result_array() as $row)
		{
			$name=$row['name'];
			$tags=$row['tags'];
			$html=$row['html'];
                        $color=$row['color'];
                        $id=$row['id'];
                        $sit_id=$row['situation_Id'];
                        $flag=$row['flag'];
                        $cflag=explode ( "_" , $flag );
                    if($cflag[0]=="duplicate"){
                                $to_load_id=$cflag[1];
                                //echo $to_load_id;
                                $dcontext=$this->Context->getContext($to_load_id);
                                if($dcontext->num_rows() !=0){
                                    $dcontext=$dcontext->row();
                                    $dhtmlt=$dcontext->html;
                                    $this->load->library('simple_html_dom');
                                    $dhtml=new simple_html_dom();
                                    $dhtml->load($dhtmlt);
                                    //The current duplicated context
                                    $complete_html=new simple_html_dom();
                                    $complete_html->load($html);
                                    //name of outer of current
                                    $cname="o_".$name;
                                    //name of inner of original
                                    $dvname=$dcontext->name;
                                    $ec=$complete_html->find("div[id=$cname]",0);
                                    $ed=$dhtml->find("div[id=$dvname]",0);
                                    //echo $dvname;
                                    $newHTML= str_replace($dvname, $name, $ed->outertext);
                                    $ec->innertext=$newHTML . $ec->innertext;
                                    $html=$complete_html->save();
                                }
                                else{
                                    $this->Context->deleteContext($id);
                                    $cflag[0]!="del";
                                }
                            }

                        $data = array(
                            'id'=> $id,
                            //'html'=> $html,
                            'tags'=> $tags,
                            'name'=> $name,
                            'color'=> $color,
                            'flag'=> $flag,
                            'situation_id'=> $sit_id
			);
                        if($cflag[0]!="del"){
                            array_push($array_of_context,$data);
                            $all_html_contxs=$all_html_contxs.$html;
                        }
                            
		}
                $context_json=json_encode($array_of_context);
                $temporary_context=array(
                  'contexts'=>  $context_json,
                  'html_sit'=> $all_html_contxs,
                  'username'=> $this->session->userdata('username'),
                  'situation_id' => $param_rec_sit,
                  'back_img'=>$back_img,
                  'height'=>$height
                );
                $this->load->view('viewpagefriendV',$temporary_context);
                //$this->load->view('header\editor');
                //$sit_json=json_encode($sit_data);
                //$context_json=json_encode($array_of_context);
		//echo $context_json."###".$sit_json;
        }

        function  copyContext()//friend
        {
            $type=1;
            $contextId=$_POST['contextId'];
            $cname=$_POST['cname'];
            $cctag=$_POST['cctag'];
            $ccflag=$_POST['ccflag'];
            $cccolor=$_POST['cccolor'];
            //$sitId_from=$_POST['sitId_from'];
            $sitId_to=$_POST['sitId_to'];
            $cHtml=$_POST['cHtml'];
            $cOld_name=$_POST['old_name'];


            $this->load->helper('date');
            $this->load->library('simple_html_dom');
            //$mhtml=$obj->contexts[$i]->html;
            $html=new simple_html_dom();
            $html->load($cHtml);
            $name=$cOld_name;

            $now = time();
            $human = unix_to_human($now);
            $unix = human_to_unix($human);

            $obj->contexts[$i]->name=$name.'_'.$unix;

            $html->find("div[id=o_$name]",0)->onmouseover="show('".$cname.'_'.$unix."')";
            $html->find("div[id=o_$name]",0)->onmouseout="hide('".$cname.'_'.$unix."')";
            $html->find("div[id=$name]",0)->ondblclick="createEditor('".$cname.'_'.$unix."')";

            $html->find("img[id=imgid_cut_$name]",0)->onmouseover="imgHover('imgid_cut_".$cname.'_'.$unix."','cutHover.png')";
            $html->find("img[id=imgid_cut_$name]",0)->onmouseout="imgHover('imgid_cut_".$cname.'_'.$unix."','cut.png')";
            $html->find("img[id=imgid_cut_$name]",0)->onclick="cut('".$cname.'_'.$unix."')";
            //need to change onclick with the new name :(

            $html->find("img[id=imgid_del_$name]",0)->onmouseover="imgHover('imgid_del_".$cname.'_'.$unix."','closeHover.png')";
            $html->find("img[id=imgid_del_$name]",0)->onmouseout="imgHover('imgid_del_".$cname.'_'.$unix."','download.png')";
            $html->find("img[id=imgid_del_$name]",0)->onclick="DeletetContext('".$cname.'_'.$unix."')";

            $html->find("img[id=imgid_mod_$name]",0)->onmouseover="imgHover('imgid_mod_".$cname.'_'.$unix."','modHover.png')";
            $html->find("img[id=imgid_mod_$name]",0)->onmouseout="imgHover('imgid_mod_".$cname.'_'.$unix."','mod.png')";
            $html->find("img[id=imgid_mod_$name]",0)->onclick="edit_context('".$cname.'_'.$unix."')";

            $html->find("img[id=imgid_copy_$name]",0)->onmouseover="imgHover('imgid_copy_".$cname.'_'.$unix."','copyHover.png')";
            $html->find("img[id=imgid_copy_$name]",0)->onmouseout="imgHover('imgid_copy_".$cname.'_'.$unix."','copyImg.png')";
            $html->find("img[id=imgid_copy_$name]",0)->onclick="copy('".$cname.'_'.$unix."')";

            $html->find("img[id=imgid_cut_$name]",0)->id="imgid_cut_".$cname.'_'.$unix;
            $html->find("img[id=imgid_mod_$name]",0)->id="imgid_mod_".$cname.'_'.$unix;
            $html->find("img[id=imgid_del_$name]",0)->id="imgid_del_".$cname.'_'.$unix;
            $html->find("img[id=imgid_copy_$name]",0)->id="imgid_copy_".$cname.'_'.$unix;

            $html->find("p[id=p_$name]",0)->innertext=$cname;
            $html->find("p[id=p_$name]",0)->id="p_".$cname.'_'.$unix;
            $html->find("div[id=$name]",0)->id=$cname.'_'.$unix;
            $html->find("div[id=o_$name]",0)->id="o_".$cname.'_'.$unix;
            $cHtml=$html->save();


            $this->load->model('Context');

            $data= array (
                //'id'=>$contextId,
                'situation_Id'=>$sitId_to,
                'html'=>$cHtml,
                'name'=>$cname.'_'.$unix,
                'tags'=>$cctag,
                'flag'=>$ccflag,
                'color'=>$cccolor
                );
            // IMPORTENT NOTE: xpoint ypoint and height,width have no use in the database
            // i think we should delete it
            // i will not add them in the database here


                $query=$this->Context->copyContext($data);
                //echo $query;
        }
        function duplicateContext()
        {
            $contextId=$_POST['contextId'];
            $cname=$_POST['cname'];
            $cctag=$_POST['cctag'];
            $ccflag=$_POST['ccflag'];
            $cccolor=$_POST['cccolor'];
            //$sitId_from=$_POST['sitId_from'];
            $sitId_to=$_POST['sitId_to'];
            $cHtml=$_POST['cHtml'];
            $cOld_name=$_POST['old_name'];

            //$tid=$contextId;
            $this->load->model('Context');
            //$obj->contexts[$i]->flag='duplicate_'.$obj->contexts[$i]->id;
            //$obj->contexts[$i]->id='';
            $this->load->library('simple_html_dom');
            //$mhtml=$obj->contexts[$i]->html;
            $html=new simple_html_dom();
            $html->load($cHtml);
            $name=$cOld_name;
            $html->find("div[id=$name]",0)->outertext='';
			
            //echo $e[0] ;
            //$e=$e[0];
            //$e->;

            //$name="o_".$name;
            //$html->find("div[id=$name]",0)->setAttribute("onload" , "loadDuplicate($tid);");
            //$e=$e[0];
            //$e;
            //echo print_r($e);
            //echo $html->save();
            $this->load->helper('date');
            $now = time();
            $human = unix_to_human($now);
            $unix = human_to_unix($human);
            $html->find("p[id=p_$name]",0)->innertext=$cname;
            $html->find("p[id=p_$name]",0)->id="p_".$cname.'_'.$unix;

            //$name=$cname.'_'.$unix;

            $chtml=$html->save();

            $newHTML= str_replace($name, $cname.'_'.$unix, $chtml);
            //$newHTML= str_replace($obj->contexts[$i]->name, $param_new_context_copy_name, $newHTML);
            //$obj->contexts[$i]->html=$newHTML;
            //$obj->contexts[$i]->name=$name;
            $data= array (
                'id'=>'',
                'situation_Id'=>$sitId_to,
                'html'=>$newHTML,
                'name'=>$cname.'_'.$unix,
                'tags'=>$cctag,
                'flag'=>$ccflag,
                'color'=>$cccolor
                );

            $this->Context->creatContext($data);
        }
}